package com.ifdom.objecttype.atomicinteger;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程安全类 AtomicInteger
 * vs
 * 非线程安全 Integer
 *
 * @Author ifredomvip@gmail.com
 * @Date 2022/9/30 15:15
 * @Version 1.0.0
 * @Description
 **/
public class AtomicIntegerDemo implements Runnable {

    private AtomicInteger count = new AtomicInteger(0);

    // 故意将申明与赋值拆分，仅作为记录
//    static {
//        count = new AtomicInteger(0);
//    }

    public AtomicIntegerDemo(AtomicInteger count) {
        this.count = count;
    }

    public void increment() {
        count.getAndIncrement();
    }

    public Integer getCount() {
        return count.get();
    }

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            increment();
        }
    }
}
